﻿@model TpWeb.Models.AddressViewModel
@{
    ViewBag.Title = "Addresses";
}

@helper TextBoxFormControl(System.Linq.Expressions.Expression<Func<TpWeb.Models.AddressViewModel, string>> field)
{
    <div class="form-group">
        @Html.LabelFor(field, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(field, new { @class = "form-control" })
        </div>
    </div>
}

<h2 id="ttl">Ваши координаты</h2>


@using (Html.BeginForm("Index", "Address", FormMethod.Post, new { id = "addrForm", @class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary()
    <fieldset>
        <legend>Адрес Регистрации</legend>
        @TextBoxFormControl(m => m.Region)
        @TextBoxFormControl(m => m.District)
        @TextBoxFormControl(m => m.LagerLocality)
        @TextBoxFormControl(m => m.SmallLocality)
        @TextBoxFormControl(m => m.Street)
        @TextBoxFormControl(m => m.PostalCode)
        @* @TextBoxFormControl(m => m.MiddleName)
            @TextBoxFormControl(m => m.LastName)
            @DateFormControl(m => m.BirthDate)
            @TextBoxFormControl(m => m.Nationality)
            @Html.HiddenFor(m => m.NationalityCode)*@
    </fieldset>

}
<div>
    Дебаг
    <ul id="dbg"></ul>
</div>
@section Scripts{
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jquery-ui")
    @Scripts.Render("~/bundles/views/addr.index")

    <script>
        function Debug(info) {
            $("<li>").text(info).appendTo("#dbg");
        }

        $(function () {
            /*-----------------------------------------------*/
            $("#Region").autocomplete({
                source: function (request, response) {
                    ajaxGetSource('GetRegions', request.term, 20, response);
                },
                minLength: 2,
                delay: 500,
                select: function (event, ui) {
                    // debugger;
                    onSelect($(this), ui.item);
                },
                open: function () {
                    $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                },
                close: function () {
                    $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                },
                change: function () {
                    clearChildren($(this));
                }
            });

            /*----------------------------------------------*/
            $("#District")
                .autocomplete({
                    source: function (request, response) {
                        var pguid = $("#Region").attr("aoguid");
                        $("#ttl").text(pguid);
                        ajaxGetSource('GetDistricts', request.term, 20, response, pguid);
                    },
                    minLength: 2,
                    delay: 500,
                    select: function (event, ui) {
                        onSelect($(this), ui.item);
                    },
                    open: function () {
                        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                    },
                    close: function () {
                        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                    },
                    change: function () {
                        clearChildren($(this));
                        // findAndSet($("#Region"),getPGuid())
                    }

                });


            /*---------------------------------------------------*/
            $("#LagerLocality")
                .autocomplete({
                    source: function (request, response) {
                        var pguid = $("#District").attr("aoguid");
                        var regCode = $("#Region").attr("regCode");
                        $("#ttl").text(pguid + ' ----- ' + regCode);
                        ajaxGetSource('GetTowns', request.term, 20, response, pguid, regCode);
                    },
                    minLength: 2,
                    delay: 500,
                    select: function (event, ui) {
                        onSelect($(this), ui.item);
                    },
                    open: function () {
                        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                    },
                    close: function () {
                        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                    },
                    change: function () {
                        clearChildren($(this));
                    }
                });

            /*---------------------------------------------------*/
            $("#SmallLocality")
               .autocomplete({
                   source: function (request, response) {
                       var pguid = $("#LagerLocality").attr("aoguid");
                       var regCode = $("#Region").attr("regCode");
                       $("#ttl").text(pguid + ' ----- ' + regCode);
                       ajaxGetSource('GetLocality', request.term, 20, response, pguid, regCode);
                   },
                   minLength: 2,
                   delay: 500,
                   select: function (event, ui) {
                       onSelect($(this), ui.item);
                   },
                   open: function () {
                       $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                   },
                   close: function () {
                       $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                   },
                   change: function () {
                       clearChildren($(this));
                       findAndSet($("#District"), getPGuid($(this)));
                   }
               });

            /*---------------------------------------------------*/
            $("#Street")
               .autocomplete({
                   source: function (request, response) {
                       var pguid = getPGuidForStreet();
                       var regCode = $("#Region").attr("regCode");
                       $("#ttl").text(pguid + ' ----- ' + regCode);
                       ajaxGetSource('GetStreets', request.term, 20, response, pguid, regCode);
                   },
                   minLength: 2,
                   delay: 500,
                   select: function (event, ui) {
                       onSelect($(this), ui.item);
                   },
                   open: function () {
                       $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
                   },
                   close: function () {
                       $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
                   },
                   change: function () {
                       //clearChildren($(this));
                       //findAndSet($("#District"), getPGuid($(this)));
                   }
               });
        });
    </script>
}